home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 89 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  62 lines

  1. Path: news.ti.com!usenet
  2. From: srikumar@india.ti.com (Srikumar K.P)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Arguments with streams
  5. Date: 01 Jan 1996 22:33:44 +0500
  6. Organization: Texas Instruments (India)
  7. Sender: srikumar@mmplsparc1.india.ti.com
  8. Message-ID: <5o68evn7k7.fsf@mmplsparc1.india.ti.com>
  9. References: <4c85u1$cga@ralph.vnet.net>
  10. NNTP-Posting-Host: mmplsparc1.india.ti.com
  11. In-reply-to: jason@char.vnet.net's message of 1 Jan 1996 08:27:44 GMT
  12. X-Newsreader: (ding) Gnus v0.99.27
  13.  
  14.  
  15. jason@char.vnet.net wrote:
  16. >I've got a small problem.  I'm working on a C++ library for HTML forms
  17. >and wanted to add the some functionality to 'cout'  (ostream), but
  18. >am stumped.
  19.  
  20. >Here's what I want:
  21.  
  22. >    cout << A("http://www.vnet.net") << "Vnet" << endA << endl;
  23.  
  24. >ostream& endA (ostream& f)
  25. >    { f << "</a>"; return f; };
  26. >ostream& A(ostream& f, char *s)
  27. >    { f << "<a " << s << ">"; return f; }
  28.  
  29. >My problem lies with 'A'.  Is there any way to say that the first argument
  30. >to the function is the ostream at the beginning of the statement without
  31. >explicitly adding it (ie. cout << A(cout, "htt...") << ... << endl; works).
  32.  
  33. >I've looked briefly at the way setw is implemented both in g++ and MSVC,
  34. >but the code looks over my head =)
  35.  
  36. The method for doing this is the IOSTREAM manipulators.
  37. Using manipulators it is possible to insert a vareity
  38. of objects into the stream along with any other types.
  39.  
  40. declare a class like this :
  41.  
  42. class A { };
  43.  
  44. ostream& operator<< (ostream &os, A a)
  45. {
  46.  // do whatever u want on os using A
  47.  return os
  48. }
  49.  
  50. Now u can use a code like this ->
  51.  
  52. int y;
  53.  
  54. cout << A("any") << y << "\n";
  55.  
  56. Go thru IoManipulators as specified in C++ Programming
  57. language Book by Stroustrop to get a better idea ..
  58.  
  59. Regards srikumar
  60.  
  61.  
  62.